package com.enoram.gwt.desktop.server;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.codec.Base64;
import org.apache.shiro.subject.Subject;

import com.enoram.gwt.desktop.client.LoginService;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class LoginImpl extends RemoteServiceServlet implements LoginService {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
 
	@Override
	public String login(String userName, String password) {
		userName = "admin"; password = "password";
		Subject currentUser = SecurityUtils.getSubject();
		
		System.out.println("User "+ currentUser);
		
		String pass = Base64.encodeToString(password.getBytes());
		String decodePass = Base64.decodeToString(pass);
		
		UsernamePasswordToken upToken =  new UsernamePasswordToken(userName, decodePass);
		
		currentUser.login(upToken);
		
		currentUser = SecurityUtils.getSubject();
		
		return Base64.encodeToString(userName.getBytes()) + ":"+ Base64.encodeToString(password.getBytes());
	}

}
